package Program.UserUI;

import Program.Information.User;
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import java.awt.*;

public class UserPaneSon extends JPanel {
    private User user;
    private JScrollPane jScrollPane;
    private JTable table;
    private DefaultTableModel dtm;
    private String[] titles = {"资料","详细信息"};
    private Object[][] data;

    public UserPaneSon() {
        super(null);
        setBounds(0,0,900,600);

    }

    public UserPaneSon(User user) {
        super(null);
        this.user=user;
        setBounds(0, 0, 900, 600);
        setBackground(Color.WHITE);
        add(initScrollPane());
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(new ImageIcon("Application_images\\userBG1.png").getImage(), 0, 0,this.getWidth(), this.getHeight(), this);
    }

    private JScrollPane initScrollPane() {
        jScrollPane=new JScrollPane();
        jScrollPane.getViewport().setOpaque(false);
        jScrollPane.setOpaque(false);
        jScrollPane.setBounds(200, 50, 500, 390);
        jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        JTable viewTable = getViewTable();
        jScrollPane.setViewportView(viewTable);
        return jScrollPane;
    }

    private JTable getViewTable() {
        data = createArr();
        dtm = new DefaultTableModel(data, titles);
        table = new JTable(dtm) {
            // 重写这个表格的方法：设置不可编辑，但可以选中
            @Override
            public boolean isCellEditable(int row, int column) {
               return false;
            }
        };
        table.getTableHeader().setFont(new Font("微软雅黑",Font.BOLD,16));
        table.setFont(new Font("微软雅黑",Font.PLAIN,20));
        table.setRowHeight(40);
        table.getTableHeader().setReorderingAllowed(false);// 设置列不可拖动
        // 设置居中
        DefaultTableCellRenderer renderer=new DefaultTableCellRenderer();
        renderer.setHorizontalAlignment(JLabel.CENTER);
        renderer.setOpaque(false);
        table.setDefaultRenderer(Object.class, renderer);
        table.setOpaque(false);	// 设置表格本身透明
        table.getTableHeader().setOpaque(false);	//设置透明
        return table;
    }

    private Object[][] createArr(){
        Object[][] arr={
                {"用户名",this.user.getUserName()},
                {"用户密码",this.user.getUserPassword()},
                {"业主姓名",this.user.getName()},
                {"手机号码",this.user.getPhone()},
                {"性别",this.user.getUserSex()},
                {"居住类型",this.user.getUsertype()},
                {"预存款",this.user.getAdDeposit()},
                {"门牌信息",this.user.getPlace()},
                {"可用状态",this.user.getState().equals("1")?"是":"否"}
        };
        return arr;
    }
}
